package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalPublishBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/12/25 0025.
 */

public class PersonalPublishModel implements PersonalPublishModelImpl{

    private static final String TAG = "PersonalPublishModel";
    private OnPersonalPublishRequestListener mListener;

    public PersonalPublishModel(OnPersonalPublishRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getPersonalPublishData(String uid, int pageSize, int page, int type) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("pageSize", String.valueOf(pageSize))
                .addFormDataPart("page", String.valueOf(page))
                .addFormDataPart("type", String.valueOf(type))
                .build();
        Observable<PersonalPublishBean> observable = DataManager.getPersonalPublish(body);
        Observable<PersonalPublishBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<PersonalPublishBean>() {
            @Override
            public void onNext(PersonalPublishBean bean) {
                if (mListener != null) {
                    mListener.onRequestSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    mListener.onRequestError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getDeleteMyInterestResult(String uid, String vid, final int position) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("id", vid)
                .build();
        Observable<CodeBean> observable = DataManager.getMyVideoDeleteResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onDeleteSuccess(bean, position);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onDeleteError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDeleteMyDiscoveryResult(String id, final int position) {
        Observable<CodeBean> observable = DataManager.getDelMyDiscoveriesRequestResponse(
                id
        );
        Observable<CodeBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onDeleteSuccess(bean, position);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.toString(), TAG);
                    mListener.onDeleteError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDeleteMyActivityResult(String id, final int position) {
        Observable<CodeBean> observable = DataManager.getDelMyActivityRequestResponse(
                id
        );
        Observable<CodeBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onDeleteSuccess(bean, position);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.toString(), TAG);
                    mListener.onDeleteError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnPersonalPublishRequestListener{
        void onRequestSuccess(PersonalPublishBean bean);
        void onRequestError(Throwable throwable);
        void onDeleteSuccess(CodeBean bean, int position);
        void onDeleteError(Throwable throwable);
    }
}
